Program used: Dev C++ Version 5.7.1 Build time: July 29 2014 - 23:53, at Windows 8.
compiler set to configure: TDM-GCC 4.8.1 64-bit release
I'm a first year student in computer engineering(undergraduate), have had my hands on C for the past 2/3months. Technically, sometimes I don't even understand what I'd typed and if it's valid or not.
The below is a homework given by my lecturer. I managed to type it, but I hope to learn if there's any better way to get the name which scanf wouldn't run through as it stops at white space. And I used pointer(or is it not?) to make it one part only alphabets and the other one digits. I'd like to know if there's better practice. I'm sorry to admit that I'm still new to this area and would really want to learn more, thanks.
The coding, calculate_charges.c and the open file, customer.txt are attached at the end of the post.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 3
void trimback(char input[], int strnameindex);
void trimfrnt(char input[], int strnameindex);
float calculate_charge (float time_start, float total_hour);
float average_charge (float total_charge, float total_hour);
int main()
{
FILE *fileread, *filewrite;
int namesize, len;
int i, str_i, str_nameindex[SIZE] = {0};
float time_start[SIZE], total_hour[SIZE], total_charge[SIZE], charge_per_hour[SIZE];
char name[SIZE][9], input[SIZE][50], namebuffer[SIZE][50];
//1openfile
fileread = fopen("customer.txt", "r");
for(i=0; fgets(input[i], 50, fileread) != NULL; i++)
{
for(str_i = 0; !isdigit(input[i][str_i]) ; str_i++)
{
str_nameindex[i]++;
}
strcpy(namebuffer[i], input[i]);
trimback(namebuffer[i], str_nameindex[i]);
strcpy(name[i], namebuffer[i]);
trimfrnt(input[i], str_nameindex[i]);
sscanf(input[i], "%f %f", &time_start[i], &total_hour[i]);
}
fclose (fileread);
//0openfile
//1calculation
for (i=0; i<SIZE ; i++)
{
total_charge[i] = calculate_charge(time_start[i], total_hour[i]);
charge_per_hour[i] = average_charge(total_charge[i], total_hour[i]);
}
//0calculation
//1savefile
filewrite = fopen("charges.txt", "w");
fprintf(filewrite, "Name total charge charge per hour\n");
for(i=0; i<SIZE ; i++)
{
fprintf(filewrite, "%-12sRM%-15.2fRM%.2f\n", name[i], total_charge[i], charge_per_hour[i]);
}
fclose (filewrite);
//0savefile
return 0;
}
void trimback(char input[], int strnameindex)
{
char *ary;
int i;
int len;
len = strlen(input);
ary = input;
*(ary + i + strnameindex) = *(ary + len);
}
void trimfrnt(char input[], int strnameindex)
{
char *ary;
int i;
int len;
len = strlen(input);
ary = input;
for(i=0; i<len; i++)
*(ary + i) = *(ary + i + strnameindex);
}
float calculate_charge (float time_start, float total_hour)
{
float total_charge;
if(time_start >= 9.00 && time_start<= 21.00)
{
if(total_hour * 100 - 1000 <= 0)
{
total_charge = 9.50;
}
else if(total_hour * 100 - 1000 > 0)
{
total_charge = 9.50 + ((int)total_hour-10+1)*1.50;
}
}
else
{
if(total_hour * 100 - 1000 <= 0)
{
total_charge = 9.50;
total_charge *= 0.9;
}
else if(total_hour * 100 - 1000 > 0)
{
total_charge = 9.50 + ((int)total_hour-10+1)*1.50;
total_charge *= 0.9;
}
}
return total_charge;
}
float average_charge (float total_charge, float total_hour)
{
float average_charge;
average_charge = total_charge / total_hour;
return average_charge;
}
Code:
customer.txt
Ahmad 09.00 4.2
Mei Lin 13.00 12.5
Julia 22.30 14.5
Attachment 13791
Attachment 13792